<template>
  <d2-container>

    <template slot="header">
      <el-form :gutter="24" ref="search-box" :model="mainQuery"  label-position="left" :inline="true">
        <el-form-item label="通道名称：" size="mini">
          <el-input placeholder="" v-model="mainQuery.channelName" @keyup.enter.native="fetchMainData" size="small" clearable></el-input>
        </el-form-item>
      </el-form>

      <div class="search-btn-box">
        <div class="left">
          <el-button-group>
            <el-button class="filter-item" icon="el-icon-circle-plus" type="success" @click="handleAdd" :loading="mainTable.loading" size="mini">添加</el-button>
            <el-tooltip :disabled="!!mainTable.current" content="请选择一行数据">
              <el-button class="filter-item" icon="el-icon-edit" type="default" @click="handleSet" :loading="mainTable.loading" size="mini">修改</el-button>
            </el-tooltip>
          </el-button-group>
        </div>
        <div class="right">
          <el-button class="filter-item" icon="" type="default" @click="resetMainData" :loading="mainTable.loading" size="small">重置</el-button>
          <el-button class="filter-item" icon="el-icon-search" type="primary" @click="fetchMainData" :loading="mainTable.loading" size="small">查询</el-button>
        </div>
      </div>
    </template>

    <!--主列表数据-->
    <d2-crud
      ref="d2Crud"
      :options="mainTable.options"
      :index-row="mainTable.indexRow"
      :columns="mainTable.columns"
      :data="mainTable.list"
      :selection-row="mainTable.allowSelect"
      :loading="mainTable.loading"
      @current-change="onMainCurrentChange"
      @row-dblclick="onMainDoubleClick"
      @selection-change=""
    ></d2-crud>

    <template slot="footer">
      <el-pagination
        @size-change="handleMainSizeChange"
        @current-change="handleMainPageChange"
        :current-page="mainTable.pagination.currentPage"
        :page-sizes="mainTable.pagination.pageSizes"
        :page-size="mainTable.pagination.pageSize"
        :page-count="mainTable.pagination.pageCount"
        :total="mainTable.pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        :background="true"
        >
      </el-pagination>
    </template>

    <dialog-add ref="dialog-add" @dialog-close="fetchMainData"></dialog-add>

  </d2-container>
</template>

<script>

import ApiMain from '@/api/formula';
import DialogAdd from './dialog-add';
import { diffFieldsOptions } from '@/const/constant'
import util from '@/libs/util.js';

export default {
  name: 'bank-commissionFormula',
  mixins: [
  ],
  components: {
    'dialog-add': DialogAdd,
  },
  data () {
    return {

      mainQuery: {
        type: 4,
        channelName: '',
      },

      mainTable: {
        loading: false,
        options: {
          border             : true,
          stripe             : true,
          highlightCurrentRow: true,
          emptyText          : '暂无数据',
          size               : 'mini',
        },
        // indexRow: {
        //   title:'序号',
        //   width: "80",
        //   align:'center'
        // },
        indexRow: null,
        allowSelect: false,
        columns: [
          // {title: 'id', key: 'id', align: 'center', showOverflowTooltip: true},
          {title: '通道名称', key: 'channelName', align: 'center', showOverflowTooltip: true},
          {title: '返佣人', key: 'channelIntroducerName', align: 'center', showOverflowTooltip: true},
          {title: '创建人', key: 'createBy', align: 'center', showOverflowTooltip: true},
          {title: '创建时间', key: 'createTime', align: 'center', showOverflowTooltip: true},
        ],
        list: [],
        pagination: {
          currentPage: 1,
          pageSize   : 50,
          total      : 0,
          pageSizes  : [50, 100, 200]
        },
      },

      diffFieldsOptions: diffFieldsOptions,
    }
  },
  mounted () {
    this.resetMainData();
  },
  beforeDestroy () {
  },
  methods: {

    renderMainData (re) {
      this.mainTable.list = re.list;
      this.mainTable.pagination.total = re.total;
    },

    handleMainSizeChange (val) {
      this.mainTable.pagination.pageSize = val;
      this.fetchMainData()
    },
    handleMainPageChange (val) {
      this.mainTable.pagination.page = val;
      this.fetchMainData()
    },

    buildMainQuery () {

      var query = {
        page  : this.mainTable.pagination.currentPage,
        limit : this.mainTable.pagination.pageSize,
        search: this.mainQuery,
      };

      return query;
    },

    resetMainData () {

      this.mainQuery = {
        type: 4,
        channelName: '',
      };

      this.fetchMainData();
    },

    async fetchMainData () {

      this.mainTable.loading = true;

      try {
        var re = await ApiMain.list(this.buildMainQuery());
        this.renderMainData(re);
      } catch (err) {}

      this.mainTable.loading = false;

    },

    onMainCurrentChange (now, old) {
      this.mainTable.current = now;
    },

    onMainDoubleClick (now) {
      this.mainTable.current = now;
      this.handleSet();
    },

    handleAdd () {
      this.$refs['dialog-add'].show();
    },

    handleSet () {
      if (this.mainTable.current) {
        this.$refs['dialog-add'].show(this.mainTable.current.id);
      }
    },

  }
}
</script>

<style>

</style>
